﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InputProductForm
{
    public partial class frmAddCat : UTLBASEFORM.MDIDIALOG2
    {
        TProductType tProdType = new TProductType();
        Info.InfoCat info= new InputProductForm.Info.InfoCat();
        TCat tcat = new TCat();
        DataTable dataCat = new DataTable();
        public frmAddCat()
        {
            InitializeComponent();
        }
        public void LoadProductType()
        {
            DataTable data = tProdType.GetProType();
            cmbProType.DisplayMember = data.Columns["Name"].ToString();
            cmbProType.ValueMember = data.Columns["ID"].ToString();
            cmbProType.DataSource = data;

        }

        private void cmdAddTypePro_Click(object sender, EventArgs e)
        {
            frmAddMat frm = new frmAddMat();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
                       
            frm.ShowDialog();
        }
        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadProductType();
        }

        private void frmAddCat_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            LoadProductType();
            
        }
        private void SetupMenuBar()
        {
            ShowTBTN1(UTLBASEFORM.MDICHILD.TextLabelAdd, UTLBASEFORM.MDICHILD.ImageNameCheck_Ok);
            ShowTBTN2(UTLBASEFORM.MDICHILD.TextLabelEdit, UTLBASEFORM.MDICHILD.ImageNameEdit);
            ShowTBTN3("Hủy", UTLBASEFORM.MDICHILD.ImageNameCancel);

        }
        private void LoadCat(int id)
        {

            dataCat = tcat.GetCat(id);
            lvCat.Items.Clear();
            string active;
            for (int i = 0; i < dataCat.Rows.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());
                lvCat.Items.Add(item);
                item.SubItems.Add(dataCat.Rows[i]["Name"].ToString());
                item.SubItems.Add(dataCat.Rows[i]["Symbol"].ToString());
                if (dataCat.Rows[i]["Active"].ToString() == "True")
                    active = "Hoạt động";
                else active = "Khóa";
                item.SubItems.Add(active);

                if (i % 2 == 0)
                {
                    item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                {
                    item.BackColor = Color.AntiqueWhite;
                    //return Brushes.Beige;
                }

            }

        }
        public Info.InfoCat SetInfo()
        {
            info.Name = txtName.Text;
            info.Symbol = txtSymbol.Text;
            info.IDType = int.Parse(cmbProType.SelectedValue.ToString());
            if (cmbActive.SelectedText.Trim() == "Khóa")
                info.Active = false;
            else info.Active = true;
            return info;
        }
        protected override void tbtn1_Click(object sender, EventArgs e)
        {
            SetInfo();
            tcat.Insert(info);
            LoadCat(int.Parse(cmbProType.SelectedValue.ToString()));
            RefeshForm();
        }
        public void UpdateProType()
        {
            SetInfo();
            tcat.Update(info);
            LoadCat(int.Parse(cmbProType.SelectedValue.ToString()));
            // RefeshForm();
        }
        protected override void tbtn2_Click(object sender, EventArgs e)
        {
            UpdateProType();
            ChangeButton("Insert");
        }
        protected override void tbtn3_Click(object sender, EventArgs e)
        {
            ChangeButton("Insert");
        }
        private void ChangeButton(string state)
        {
            if (state == "Insert")
            {
                HideTBTN3();
                HideTBTN2();
                ShowTBTN1(UTLBASEFORM.MDICHILD.TextLabelAdd, UTLBASEFORM.MDICHILD.ImageNameCheck_Ok);
                RefeshForm();
            }
            else if (state == "Update")
            {

                HideTBTN1();
                ShowTBTN2(UTLBASEFORM.MDICHILD.TextLabelEdit, UTLBASEFORM.MDICHILD.ImageNameEdit);
                ShowTBTN3("Hủy", UTLBASEFORM.MDICHILD.ImageNameCancel);

            }
        }
        public void RefeshForm()
        {
            txtName.Text = "";
            txtSymbol.Text = "";
        }
        public void ShowListToForm(int id)
        {
            info.ID = int.Parse(dataCat.Rows[id]["ID"].ToString());
            txtName.Text = dataCat.Rows[id]["Name"].ToString();
            txtSymbol.Text = dataCat.Rows[id]["Symbol"].ToString();
            if (dataCat.Rows[id]["Active"].ToString() == "True")
                cmbActive.Text = "Hoạt động";
            else cmbActive.Text = "Khóa";
        }

        private void cmbProType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCat(int.Parse(cmbProType.SelectedValue.ToString()));
        }

        private void lvCat_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeButton("Update");
            ListView lv = (ListView)sender;

            if (lv.Items.Count > 0)
            {
                int flag = -1;
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (lv.Items[i].Selected)
                    {
                        flag = i;
                    }
                }
                if (flag != -1)
                {

                    ShowListToForm(flag);

                }
            }
        }
    }
}
